package com.gt.quality.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.quality.config.SpringBeanUtil;
import com.gt.quality.system.entity.SysMenu;
import com.gt.quality.system.entity.SysRole;
import com.gt.quality.base.pojo.LayGradleSelector;
import com.gt.quality.system.entity.vo.TreeTableMenuVo;
import com.gt.quality.system.mapper.SysMenuMapper;
import com.gt.quality.base.pojo.BaseLimitRequest;
import com.gt.quality.base.pojo.LayerListViewObject;
import com.gt.quality.system.service.SysMenuService;
import com.gt.quality.utils.CreateTreeUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 菜单权限表(SysMenu)表服务实现类
 *
 * @author makejava
 * @since 2026-11-25 16:30:08
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;


    /**
     * Description:  分页查询
     *
     * @param limitRequest 分页查询参数
     * @author: makeJava
     * @date: 2026-02-02 14:57:06
     * @return:com.baomidou.mybatisplus.core.metadata.IPage
     */
    @Override
    public IPage<SysMenu> queryLimitPage(BaseLimitRequest<SysMenu> limitRequest) {
        SysMenu requestBody = limitRequest.getRequestBody();
        IPage<SysMenu> page = getByPageParam(limitRequest);
        JSONObject thisObj = JSONObject.from(requestBody);
        QueryWrapper<SysMenu> queryWrapper = queryWrapperBuilder(thisObj, limitRequest);
        return sysMenuMapper.selectPage(page, queryWrapper);
    }

    /**
     * Description:  批量新增优化版
     *
     * @param entityList entityList
     * @author: makeJava
     * @date: 2026-02-02 19:54:59
     */
    @Override
    public Integer saveBatchByEasyBaseMapper(List<SysMenu> entityList) {

        return sysMenuMapper.insertBatchSomeColumn(entityList);
    }

    @Override
    public List<SysMenu> selectByRoleIds(List<SysRole> list) {
        return sysMenuMapper.selectByRoleIds(list);
    }

    @Override
    public LayerListViewObject buildTreeDeptInfo(List<SysMenu> list, Long menuId) {
        LayerListViewObject viewObject = new LayerListViewObject();
        List<LayGradleSelector> treeVoList = new ArrayList<>(list.size());
        for (SysMenu item : list) {
            LayGradleSelector treeVo = CreateTreeUtils.buildSelectTreeVo(item.getMenuId(), item.getParentId(), item.getMenuName());
            treeVoList.add(treeVo);
        }
        List<LayGradleSelector> allMenu = CreateTreeUtils.createSelectTree(treeVoList);
        viewObject.setNextLayer(allMenu);
        if (menuId != null) {
            SysMenu thisMenu = sysMenuMapper.selectById(menuId);
            viewObject.setCurrentLayer(Optional.ofNullable(thisMenu).orElse(new SysMenu()).getMenuId());
        }
        return viewObject;
    }

    @Cacheable(value = "cacheTreeMenuQuery")
    @Override
    public List<JSONObject> cacheTreeMenuQuery() {
        List<SysMenu> list = this.listByLocalCache();
        List<JSONObject> jsonObjects = list.stream().map(item -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("menuId", item.getMenuId());
            jsonObject.put("parentId", item.getParentId());
            jsonObject.put("id", item.getComponent());
            jsonObject.put("icon", item.getIcon());
            jsonObject.put("title", item.getMenuName());
            return jsonObject;
        }).toList();
        List<JSONObject> anyThingTree = CreateTreeUtils.createAnyThingTree(jsonObjects, "parentId", "menuId");
        return CollUtil.isEmpty(anyThingTree) ? ListUtil.empty() : anyThingTree;
    }

    @CacheEvict(value = {"cacheTreeMenuQuery", "listByLocalCache"})
    @Override
    public boolean insertOrUpdate(TreeTableMenuVo sysMenu) {
        boolean operate = false;
        if (StringUtils.isNotBlank(sysMenu.getId())) {
            operate = this.updateById(sysMenu.buildConvertDto());
        } else {
            operate = this.save(sysMenu.buildConvertDto());
        }
        return operate;
    }

    @Cacheable(value = "listByLocalCache")
    @Override
    public List<SysMenu> listByLocalCache() {
        return this.list();
    }


    /**
     * 往上传递 SqlSessionFactory
     *
     * @return SqlSessionFactory
     */
    @Override
    public SqlSessionFactory getSqlSessionFactory() {
        return super.getSqlSessionFactory();
    }

    /**
     * 往上传递 Mapper
     *
     * @return Mapper
     */
    @Override
    public Class<?> getThisMapperClass() {
        return SysMenuMapper.class;
    }

    /**
     * 往上传递 Transaction
     *
     * @return Transaction
     */
    @Override
    public TransactionTemplate getSpringTx() {
        return SpringBeanUtil.getBean(TransactionTemplate.class);
    }
}


